const spawn = require('cross-spawn');
const inquirer = require('inquirer');
const chalk = require('chalk');

let gitName = spawn.sync('git', ['config', '--global', "user.name"]).stdout.toString();
let gitEmail = spawn.sync('git', ['config', '--global', 'user.email']).stdout.toString();
gitName = gitName.slice(0, gitName.length -1)
gitEmail = gitEmail.slice(0, gitEmail.length -1);
/**
 * 配置收集类
 *
 * */
module.exports = class PromptManager {
    constructor(name) {
        this.default = [];
        if (!name) {
            this.default.push({
                type: "input",
                name: "name",
                message: "input project name:"
            })
        } else {
            console.log(`${chalk.bgGreen.white(`🗃   create app：${name}      `)}`)
        }
        this.default = this.default.concat([
            {
                type: "list",
                name: "platform",
                message: "choice project platform",
                choices: [
                    "mobile",
                    "pc"
                ]
            },
            {
                type: "input",
                name: "url",
                message: "input project repository url:"
            },
            {
                type: "checkbox",
                name: "dependencies",
                message: 'choice need install dependencies',
                when: function (answers) {
                    return answers.platform === 'mobile';
                },
                choices: [
                    new inquirer.Separator('---------通用依赖---------'),
                    {
                        name: "swiper",
                        checked: true,
                    },
                    new inquirer.Separator('---------平台依赖---------'),
                    "@up366/u3-flip-sdk",
                    "@up366/u3-flip-ui",
                    "@up366/up366-logger"
                ]
            },
            {
                type: "checkbox",
                name: "dependencies",
                message: 'choice need install dependencies',
                when: function (answers) {
                    return answers.platform === 'pc';
                },
                choices: [
                    new inquirer.Separator('---------通用依赖---------'),
                    "swiper",
                    new inquirer.Separator('---------平台依赖---------'),
                    "@up366/u3-flip-sdk",
                    "@up366/u3-flip-ui-pc",
                    "@up366/up366-logger"
                ]
            },
            {
                type: "input",
                name: "version",
                message: "input project version:",
                default: "1.0.0"
            },
            {
                name: "author",
                message: "input author:",
                default: gitName
            },
            {
                name: "email",
                message: "input email:",
                default: gitEmail
            },
            {
                name: "keywords",
                message: "input keywords multiple cut with|:",
            }
        ]);
    }

    async prompt() {
        return inquirer.prompt(this.default)
    }
}
